# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=5

PYTHON_COMPAT=( python{3_1,3_2,3_3} )

inherit games python-single-r1 git-2

DESCRIPTION="Universal frontend for libretro-based emulators"
HOMEPAGE="http://themaister.net/retroarch.html"
SRC_URI=""

EGIT_REPO_URI="git://github.com/Themaister/RetroArch.git"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~x86 ~amd64 ~arm"
IUSE="alsa cg +fbo ffmpeg jack netplay openal oss pulseaudio python sdl sdl-image truetype xml xv zlib"
RDEPEND="sdl? ( >=media-libs/libsdl-1.2.10[joystick] )
	alsa? ( media-libs/alsa-lib )
	cg? ( media-gfx/nvidia-cg-toolkit )
	ffmpeg? ( media-video/libav )
	jack? ( >=media-sound/jack-audio-connection-kit-0.120.1 )
	openal? ( media-libs/openal )
	xml? ( dev-libs/libxml2 )
	truetype? ( media-libs/freetype:2 )
	pulseaudio? ( media-sound/pulseaudio )
	sdl-image? ( media-libs/sdl-image:0 )
	xv? ( x11-libs/libXv )
	zlib? ( sys-libs/zlib )
	python? ( ${PYTHON_DEPS} )"
DEPEND="virtual/pkgconfig
	${RDEPEND}"

REQUIRED_USE="|| ( alsa jack openal oss pulseaudio )
	arm? ( !cg )"

pkg_pretend()
{
	if use cg; then
		CGVALUE="/usr/include/Cg: symbolic link to \`/opt/nvidia-cg-toolkit/include/Cg'"
		if [[ $(file /usr/include/Cg) != $CGVALUE ]]; then
		    echo " * You need to symlink \"/opt/nvidia-cg-toolkit/include/Cg\""
		    echo " * to \"/usr/include/Cg\" for shader support, run:"
		    echo " * \"ln -s /opt/nvidia-cg-toolkit/include/Cg /usr/include/Cg\""
		    echo " * as root or disable USE=\"-cg\"."
		    echo " * See \"Bug 255867\" for more details."
		fi
	fi
}
pkg_setup() {
	use python && python-single-r1_pkg_setup
	games_pkg_setup
	if use cg; then
		CGVALUE="/usr/include/Cg: symbolic link to \`/opt/nvidia-cg-toolkit/include/Cg'"
		if [[ $(file /usr/include/Cg) != $CGVALUE ]]; then
		    echo "--> You need to symlink \"/opt/nvidia-cg-toolkit/include/Cg\""
		    echo "--> to \"/usr/include/Cg\" for shader support, run:"
		    echo "--> \"ln -s /opt/nvidia-cg-toolkit/include/Cg /usr/include/Cg\""
		    echo "--> as root or disable USE=\"-cg\"."
		    echo "--> See \"Bug 255867\" for more details."
		    die "Failed sanity check, see build log for full details!"
		fi
	fi
}

src_prepare() {
	epatch "${FILESDIR}/${PN}-python.patch"
	epatch "${FILESDIR}/${PN}-9999-build.patch"

	if use python; then
		PYTHON_LIBS="$(python_get_EPYTHON)"
		sed -i qb/config.libs.sh \
			-e "s:%PYTHON_VER%:${PYTHON_LIBS/python/}:" \
			|| die
	fi
}

src_configure() {
	if use cg; then
		CPPFLAGS=-I/opt/nvidia-cg-toolkit/include/Cg
		LDFLAGS=-L/opt/nvidia-cg-toolkit/$(get_libdir)
	fi
	egamesconf \
		$(use_enable alsa) \
		$(use_enable ffmpeg) \
		$(use_enable jack) \
		$(use_enable openal al) \
		$(use_enable oss) \
		$(use_enable cg) \
		$(use_enable xml libxml2) \
		$(use_enable fbo) \
		$(use_enable truetype freetype) \
		$(use_enable pulseaudio pulse) \
		$(use_enable netplay) \
		$(use_enable sdl-image sdl_image) \
		$(use_enable xv xvideo) \
		$(use_enable python) \
		$(use_enable zlib) \
		$(use_enable sdl) \
		--enable-dynamic \
		--with-man_dir="/usr/share/man/man1" \
		|| die
	# Setting /etc/retroarch.cfg core path
	cd "$S"
	sed -i "s|# libretro_path = \"/path/to/libretro.so\"|libretro_path =\
	\"/usr/$(get_libdir)/libretro\"|g" retroarch.cfg \
	|| die "replacing retroarch.cfg failed, try without the sed line above."
}

src_install() {
	emake DESTDIR="${D}" install || die
	dodoc README.md AUTHORS
	prepgamesdirs
}

pkg_postinst() {

	elog "Please take a look at /etc/retroarch.cfg"
	elog "and move all path configurations to ~/.retroarch.cfg"
	elog "You need to modify them to suit your taste. Run:"
	elog "\"retroarch-joyconfig -i ~/.retroarch.cfg -o ~/.retroarch.cfg\""
	elog "to save your gamepad configuration."

        if has_version media-libs/mesa ; then
                ewarn "media-libs/mesa needs -bindist to make retroarch work!"
        fi

}